package com.my.auth.service.service.impl;

import com.my.auth.common.enums.IsDeletedFlagEnum;
import com.my.auth.service.dao.AuthRoleDao;
import com.my.auth.service.dao.AuthUserRoleDao;
import com.my.auth.service.entity.AuthRole;
import com.my.auth.service.entity.AuthUserRole;
import com.my.auth.service.service.AuthRoleService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (AuthRole)表服务实现类
 *
 * @author makejava
 * @since 2023-11-02 23:55:19
 */
@Service("authRoleService")
public class AuthRoleServiceImpl implements AuthRoleService {
    @Resource
    private AuthRoleDao authRoleDao;
    @Resource
    private AuthUserRoleDao authUserRoleDao;

    @Override
    public Boolean add(AuthRole authRole) {
        authRole.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        int count = authRoleDao.insert(authRole);
        return count > 0;
    }

    @Override
    public Boolean update(AuthRole authRole) {
        int count = authRoleDao.update(authRole);
        return count > 0;
    }

    @Override
    public Boolean delete(AuthRole authRole) {
        authRole.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());
        int count = authRoleDao.update(authRole);
        return count > 0;
    }

    /**
     * 根据用户id获取角色列表
     * @param loginId
     * @return
     */
    @Override
    public List<AuthRole> getRoleList(Long loginId) {
        AuthUserRole authUserRole = new AuthUserRole();
        authUserRole.setUserId(loginId);
        List<AuthUserRole> authUserRoleList = authUserRoleDao.queryAllByLimit(authUserRole);
        List<Long> roleIds = authUserRoleList.stream().map(AuthUserRole::getRoleId).toList();
        return authRoleDao.queryByRoleList(roleIds);
    }

}
